package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.domain.Producter;
import com.bjsxt.dto.ProducterDto;
import com.bjsxt.mapper.ProducterMapper;
import com.bjsxt.service.ProducterService;
import com.bjsxt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

/**
 * 生产厂家service接口实现类
 */
@Service(methods = {@Method(name = "addProducter",retries = 0)})
public class ProducterServiceImpl implements ProducterService {

    @Autowired
    private ProducterMapper producterMapper;

    /**
     * 分页查询生产厂家
     *
     * @param producterDto
     * @return
     */
    @Override
    public DataGridView listProducterPage(ProducterDto producterDto) {
        Page<Producter> page = new Page<>();
        QueryWrapper<Producter> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(producterDto.getProducterName()), Producter.COL_PRODUCTER_NAME, producterDto.getProducterName());
        queryWrapper.like(StringUtils.isNotBlank(producterDto.getKeywords()), Producter.COL_KEYWORDS, producterDto.getKeywords());
        queryWrapper.eq(StringUtils.isNotBlank(producterDto.getProducterTel()), Producter.COL_PRODUCTER_TEL, producterDto.getProducterTel());
        queryWrapper.eq(StringUtils.isNotBlank(producterDto.getStatus()), Producter.COL_STATUS, producterDto.getStatus());
        queryWrapper.ge(producterDto.getBeginTime() != null, Producter.COL_CREATE_TIME, producterDto.getBeginTime());
        queryWrapper.le(producterDto.getEndTime() != null, Producter.COL_CREATE_TIME, producterDto.getEndTime());
        this.producterMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 根据producterId查询生产厂家
     *
     * @param producterId
     * @return
     */
    @Override
    public Producter getOne(Long producterId) {
        return this.producterMapper.selectById(producterId);
    }

    /**
     * 添加生产厂家
     *
     * @param producterDto
     * @return
     */
    @Override
    public int addProducter(ProducterDto producterDto) {
        Producter producter = new Producter();
        BeanUtil.copyProperties(producterDto, producter);
        producter.setCreateBy(producterDto.getSimpleUser().getUserName());
        producter.setCreateTime(DateUtil.date());
        return this.producterMapper.insert(producter);
    }

    /**
     * 修改生产厂家
     *
     * @param producterDto
     * @return
     */
    @Override
    public int updateProducter(ProducterDto producterDto) {
        Producter producter = new Producter();
        BeanUtil.copyProperties(producterDto, producter);
        producter.setUpdateBy(producterDto.getSimpleUser().getUserName());
        producter.setUpdateTime(DateUtil.date());
        return this.producterMapper.updateById(producter);
    }

    /**
     * 根据producterIds删除生产厂家
     *
     * @param producterIds
     * @return
     */
    @Override
    public int deleteProducterByIds(Long[] producterIds) {
        List<Long> ids = Arrays.asList(producterIds);
        if (ids.size() > 0) {
            return this.producterMapper.deleteBatchIds(ids);
        }
        return 0;
    }

    /**
     * 查询所有可用的生产厂家
     *
     * @return
     */
    @Override
    public List<Producter> selectAllProducter() {
        QueryWrapper<Producter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Producter.COL_STATUS, Constants.STATUS_TRUE);
        return this.producterMapper.selectList(queryWrapper);
    }
}
